package in.abip.chisame;

import in.abip.chisame.model.Remote;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Telnet activity
 * 
 * @author Julot
 * @since Friday, April 29th, 2011
 */
public class RemoteActivity extends NetworkActivity {
	public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.remote_activity);
        bindHome();
        changeTitle();
        bindButton();
	}
	
	/** Change title */
	protected void changeTitle() {
		TextView text = (TextView) findViewById(R.id.title);
		text.setText(getModel().name);
	}
	
	private Remote _model = null;
	
	protected Remote getModel() {
		if (_model == null) {
			Bundle bundle = getIntent().getExtras();
			Integer id = bundle.getInt("remoteId");
			_model = (Remote) Remote.model().findByPk(id);
		}
		return _model;
	}
	
	/** Bind home button click. */
	protected void bindHome() {
		OnClickListener listener = new OnClickListener() {
			public void onClick(View view) {
				RemoteActivity.this.finish();
			}
		};
		View view = (View) findViewById(R.id.home);
		view.setOnClickListener(listener);
	}
	
	protected void bindButton() {
		OnClickListener listener = new OnClickListener() {
			public void onClick(View view) {
				Integer value = 0;
				value = Integer.parseInt((String) view.getTag());
				Toast.makeText(
					RemoteActivity.this, 
					getModel().getRunCmd(value), 
					Toast.LENGTH_SHORT
				).show();
			}
		};
		View view = null;
		view = (View) findViewById(R.id.channel_1);
		view.setOnClickListener(listener);
		view = (View) findViewById(R.id.channel_2);
		view.setOnClickListener(listener);
		view = (View) findViewById(R.id.channel_3);
		view.setOnClickListener(listener);
		view = (View) findViewById(R.id.channel_4);
		view.setOnClickListener(listener);
		view = (View) findViewById(R.id.channel_5);
		view.setOnClickListener(listener);
		view = (View) findViewById(R.id.channel_6);
		view.setOnClickListener(listener);
		view = (View) findViewById(R.id.channel_7);
		view.setOnClickListener(listener);
		view = (View) findViewById(R.id.channel_8);
		view.setOnClickListener(listener);
		view = (View) findViewById(R.id.channel_9);
		view.setOnClickListener(listener);
		view = (View) findViewById(R.id.channel_0);
		view.setOnClickListener(listener);
		/*
		view = (View) findViewById(R.id.channel_change_digit);
		view.setOnClickListener(listener);
		view = (View) findViewById(R.id.channel_change_av);
		view.setOnClickListener(listener);
		view = (View) findViewById(R.id.volume_up);
		view.setOnClickListener(listener);
		view = (View) findViewById(R.id.volume_down);
		view.setOnClickListener(listener);
		*/
	}
}
